<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title>书名信息</title>
    <link href="css/style.css" rel="stylesheet" type="text/css"/>
    <script type="text/javascript" src="js/jquery.js"></script>
    <script type="text/javascript" src="js/My97DatePicker/WdatePicker.js"></script>
    <script type="text/javascript">

        // old write
        $(document).ready(function () {
            $(".click").click(function () {
                $(".tip").fadeIn(200);
            });

            $(".tiptop a").click(function () {
                $(".tip").fadeOut(200);
            });

            $(".sure").click(function () {
                $(".tip").fadeOut(100);
            });

            $(".cancel").click(function () {
                $(".tip").fadeOut(100);
            });
        });

    </script>
    <style>
        .desc{
            white-space: nowrap;
        }
    </style>
</head>
<body>

<div class="place">
    <span>位置：</span>
    <ul class="placeul">
        <li><a href="#">图书管理</a></li>
    </ul>
</div>

<form id="f5" method="post" onsubmit="return showInfo(1)">
    <div style="width: 100%;height: 30px; text-align: center;">
        图书名称：<input name="bname" style="height: 25px;border:1px solid #ccc;" type="text"/> &nbsp;&nbsp;

        热推：<select name="state" style="height: 28px;border:1px solid #ccc;">
        <option value="">请选择</option>
        <option value="0">正常</option>
        <option value="1">火热</option>
        <option value="2">推荐</option>
        <option value="3">天地推理</option>
    </select> &nbsp;&nbsp;
        时间：<input class="Wdate" name="start_time" type="text" style="height: 25px;border:1px solid #ccc;"
                  onclick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd HH:mm:ss'})"/> ~
        <input class="Wdate" name="end_time" type="text" style="height: 25px;border:1px solid #ccc;"
               onclick="WdatePicker({el:this,dateFmt:'yyyy-MM-dd HH:mm:ss'})"/>&nbsp;&nbsp;
        <input type="submit" value="查询" style="width: 60px;height: 30px;"/>
    </div>
    <br/>
</form>

<div class="rightinfo">

    <div class="tools">

        <ul class="toolbar">

            <!--

            <li class="click"><span><img src="images/t02.png" /></span>修改</li>
            <li><span><img src="images/t04.png" /></span>统计</li>
             -->
            <li style="cursor: pointer;" id="addBook"><span><img src="images/t01.png"/></span>添加图书</li>
            <li style="cursor: pointer;" onclick="delAll()"><span><img src="images/t03.png"/></span>批量删除</li>
        </ul>

    </div>


    <table class="tablelist">
        <thead>
        <tr>
            <th><input id="checkall" name="" type="checkbox" value="" onchange="checkall()"/></th>
            <th>序号<i class="sort"><img src="images/px.gif"/></i></th>
            <th>图书类别</th>
            <th>图书名称</th>
            <th>作者</th>
            <th>图书标价</th>
            <th width="10%" class="desc">简介</th>
            <th>热推</th>
            <th>版本</th>
            <th>出版日期</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody id="plist"></tbody>
    </table>


    <div class="pagin">
    </div>


    <div class="tip">
        <div class="tiptop"><span>提示信息</span><a></a></div>

        <div class="tipinfo">
            <span><img src="images/ticon.png"/></span>
            <div class="tipright">
                <p>是否确认对信息的修改 ？</p>
                <cite>如果是请点击确定按钮 ，否则请点取消。</cite>
            </div>
        </div>

        <div class="tipbtn">
            <input name="" type="button" class="sure" value="确定"/>&nbsp;
            <input name="" type="button" class="cancel" value="取消"/>
        </div>

    </div>


</div>

<script type="text/javascript">
    $('.tablelist tbody tr:odd').addClass('odd');

    var currentPage = 1;
    showInfo(currentPage);

    function showInfo(currentPageNum) {
        var bname = $("[name='bname']").val();
        var state = $("[name='state']").val();
        var start_time = $("[name='start_time']").val();
        var end_time = $("[name='end_time']").val();
        currentPageNum = currentPageNum <= 1 ? 1 : currentPageNum;
        currentPage = currentPageNum
        $.ajax({
            url: "bookServlet",
            type: "post",
            data: {
                mark: "selectPage", currentPageNum: currentPageNum,
                bname: bname,
                state: state,
                start_time: start_time,
                end_time: end_time
            },
            dataType: "text",
            success: function (data) {
                var page = JSON.parse(data)
                var tableTemp = ""
                var cc
                $(page.list).each(function () {
                    switch (this.state) {
                        case 0: cc='正常';break;
                        case 1: cc='火热';break;
                        case 2: cc='推荐';break;
                        case 3: cc='天地推理';break;
                    }
                    tableTemp += '<tr>\n' +
                        '            <td><input name="check" type="checkbox" value="' + this.bid + '"/></td>\n' +
                        '            <td>' + this.bid + '</td>\n' +
                        '            <td>' + this.cname + '</td>\n' +
                        '            <td>' + this.bname + '</td>\n' +
                        '            <td>' + this.author + '</td>\n' +
                        '            <td>' + this.price + '</td>\n' +
                        '            <td width="10%" class="desc">' + this.description + '</td>\n' +
                        '            <td>' + cc + '</td>\n' +
                        '            <td>' + this.version + '</td>\n' +
                        '            <td>' + this.book_date + '</td>\n' +
                        '            <td>\n' +
                        '                <a href="javascript:void(0)" onclick="delPro(' + this.bid + ')">删除</a>\n' +
                        '                <a href="javascript:void(0)" onclick="updatePro(' + this.bid + ')">修改</a>\n' +
                        '            </td>\n' +
                        '        </tr>'
                })
                $("#plist").html(tableTemp)
                var paginTemp = '<div class="message">共 <i class="blue">' + page.totalCount + '</i> 条记录，当前显示第&nbsp;<i class="blue">' + page.currentPageNum + '&nbsp;</i>页，共 <i class="blue">' + page.totalPageCount + '</i> 页</div>\n' +
                    '        <ul class="paginList">\n' +
                    '            <li class="paginItem"><a href="javascript:void(0);" onclick="showInfo(' + 1 + ')">首页</a></li>\n' +
                    '            <li class="paginItem"><a href="javascript:void(0);" onclick="showInfo(' + (page.currentPageNum > 1 ? (page.currentPageNum - 1) : 1) + ')">上一页</a></li>\n' +
                    '            <li class="paginItem"><a href="javascript:void(0);" onclick="showInfo(' + (page.currentPageNum < page.totalPageCount ? (page.currentPageNum + 1) : page.totalPageCount) + ')">下一页</a></li>\n' +
                    '            <li class="paginItem"><a href="javascript:void(0);" onclick="showInfo(' + page.totalPageCount + ')">尾页</a></li>\n' +
                    '        </ul>'
                $(".pagin").html(paginTemp)
            },
            error: function () {
                alert("请求失败")
            }
        })
        return false;
    }

    $(function () {
        $('#addBook').click(function () {
            window.location.href = "bookAdd.html";
        });
    });

    function delPro(bid) {
        $.ajax({
            url: "bookServlet",
            type: "post",
            data: {mark: "delete", ids: bid},
            dataType: "text",
            success: function (data) {
                if (data) {
                    alert("删除成功")
                    showInfo(currentPage)
                } else {
                    alert("删除失败")
                }
            },
            error: function () {
                alert("请求失败")
            }
        })
    }

    function updatePro(bid) {
        window.location.href = "bookUpdate.html?bid=" + bid;
    }

    function checkall() {
        var alls = document.getElementsByName("check");
        var ch = document.getElementById("checkall");
        if (ch.checked) {
            for (var i = 0; i < alls.length; i++) {
                alls[i].checked = true;
            }
        } else {
            for (var i = 0; i < alls.length; i++) {
                alls[i].checked = false;
            }
        }
    }

    function delAll() {
        var alls = document.getElementsByName("check");
        var ids = new Array();
        for (var i = 0; i < alls.length; i++) {
            if (alls[i].checked) {
                ids.push(alls[i].value);
            }
        }
        if (ids.length > 0) {
            if (confirm("确认删除?")) {
                delPro(ids.toString());
            }
        } else {
            alert("请选中要删除的项");
        }
    }
</script>
</body>
</html>
